package com.wanpinghui.supplier.widgets;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBean;

/**
 * Created by xiongning on 17-12-7.
 */

public class MsgLeftTxtView extends LinearLayout {

    private TextView tv_msg;

    private SpinView sv;

    private ImageView iv_error;

    private MsgBean msgBean;

    public MsgLeftTxtView(Context context) {
        super(context);
        init(context);
    }

    public MsgLeftTxtView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MsgLeftTxtView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MsgLeftTxtView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View mView = inflater.inflate(R.layout.item_msg_left_txt, this);
        tv_msg = (TextView) mView.findViewById(R.id.tv_msg);
        sv = (SpinView) mView.findViewById(R.id.sv);
        iv_error = (ImageView) mView.findViewById(R.id.iv_error);
    }

    public void initData(MsgBean msgBean) {

        this.msgBean = msgBean;
        setMsg(msgBean.getMsg());

        switch (msgBean.getSend_statu()) {
            case 0:
                setError(false);
                setSending(true);
                break;
            case 1:
                setError(false);
                setSending(false);
                break;
            case 2:
                setError(true);
                setSending(false);
                break;
        }
    }

    public void setMsg(String msg) {
        tv_msg.setText(msg);
    }

    public void setSending(boolean isSendding) {
        if (isSendding)
            sv.setVisibility(View.VISIBLE);
        else
            sv.setVisibility(View.GONE);

    }

    public void setError(boolean isError) {
        if (isError)
            iv_error.setVisibility(View.VISIBLE);
        else
            iv_error.setVisibility(View.GONE);
    }
}
